package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.input.gamepad.FlxGamepad;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxMath;
import flixel.util.FlxRandom;
import flixel.input.gamepad.LogitechButtonID;
import flixel.input.gamepad.PS4ButtonID;
import flixel.util.FlxColor;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
/**
 * A FlxState which can be used for the game's menu.
 */
class MenuState extends FlxState
{
	/**
	 * Function that is called up when to state is created to set it up. 
	 */
	
	public var text:FlxText;
	public var puntos:FlxText;
	public var estado:String;
	public var estadoInt:Int;
	public var time:Float;
	public var tilde:FlxSprite;
	
	override public function create():Void
	{
		super.create();
		FlxG.camera.fade(FlxColor.BLACK,1,true);
		var text1 = new FlxText(0, 100, 800, "Seleccion de Equipo");
		text1.setFormat("font", 40,FlxColor.WHITE,"center");
		add(text1);
		
		text = new FlxText(0, 300, 800, "EQUIPO AZUL");
		text.setFormat("font", 40,FlxColor.BLUE,"center");
		add(text);
		
		puntos = new FlxText(0, 400, 800, "");
		puntos.setFormat("font", 40,FlxColor.WHITE,"center");
		add(puntos);
		
		estado = "1";
		time = 0.2;
		tilde = new FlxSprite(0, 0);
		tilde.loadGraphic("assets/images/tilde.png");
		add(tilde);
		tilde.x = FlxG.width / 2 - tilde.width / 2;
		tilde.y = 380;
		tilde.scale.set(0, 0);
		estadoInt = 1;
		
		Reg.preguntas = new Array<Bool>();
		for (i in 0...100)
		{
			Reg.preguntas.push(false);
		}
		
	}
	
	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy():Void
	{
		super.destroy();
	}
	
	private function onEnd(tween:FlxTween):Void
	{
		estadoInt++;
		estado = Std.string(estadoInt);
		tilde.scale.set(0, 0);
		if (estadoInt == 5)
			FlxG.camera.fade(FlxColor.BLACK,1,false,onFade);
	}
	
	private function onFade():Void
	{
		FlxG.switchState(new TableroTestState());
	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update():Void
	{
		super.update();
		var gamepad:FlxGamepad = null;
		time -= FlxG.elapsed;
		if (time < 0 && estado != "tilde")
		{
			puntos.text += ".";
			time = 0.2;
		}
		if (puntos.text == "....")
			puntos.text = "";
		switch(estado)
		{
			case "1":
				text.text = "EQUIPO AZUL";
				gamepad = FlxG.gamepads.lastActive;
				for (gamepad in FlxG.gamepads.getActiveGamepads())
				{
					if (gamepad != null && gamepad.justPressed(LogitechButtonID.ONE))
					{
						Reg.gamepad1 = gamepad;
						estado = "tilde";
						puntos.text = "";
						var tween = FlxTween.tween(tilde.scale, { x:1, y:1 }, 1, { complete:onEnd, ease:FlxEase.bounceOut } );
						break;
					}	
				}
			
			case "2":
				text.text = "EQUIPO ROJO";
				text.color = FlxColor.RED;
				for (gamepad in FlxG.gamepads.getActiveGamepads())
				{
					if (gamepad != null && gamepad.justPressed(LogitechButtonID.ONE) && gamepad != Reg.gamepad1)
					{
						Reg.gamepad2 = gamepad;
						estado = "tilde";
						puntos.text = "";
						var tween = FlxTween.tween(tilde.scale, { x:1, y:1 }, 1, { complete:onEnd, ease:FlxEase.bounceOut } );
						break;
					}	
				}
			case "3":
				text.text = "EQUIPO VERDE";
				text.color = FlxColor.GREEN;
				for (gamepad in FlxG.gamepads.getActiveGamepads())
				{
					if (gamepad != null && gamepad.justPressed(LogitechButtonID.ONE) && gamepad != Reg.gamepad1 && gamepad != Reg.gamepad2)
					{
						Reg.gamepad3 = gamepad;
						estado = "tilde";
						puntos.text = "";
						var tween = FlxTween.tween(tilde.scale, { x:1, y:1 }, 1, { complete:onEnd, ease:FlxEase.bounceOut } );
						break;
					}	
				}
			case "4":
				text.text = "EQUIPO AMARILLO";
				text.color = FlxColor.YELLOW;
				for (gamepad in FlxG.gamepads.getActiveGamepads())
				{
					if (gamepad != null && gamepad.justPressed(LogitechButtonID.ONE) && gamepad != Reg.gamepad1 && gamepad != Reg.gamepad2 && gamepad != Reg.gamepad3)
					{
						Reg.gamepad4 = gamepad;
						estado = "tilde";
						puntos.text = "";
						var tween = FlxTween.tween(tilde.scale, { x:1, y:1 }, 1, { complete:onEnd, ease:FlxEase.bounceOut } );
						break;
					}	
				}
			
		}
		
	}	
}